home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / examples / scene.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  5KB  |  176 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. /*
  41.  *  scene.c
  42.  *  This program demonstrates the use of the GL lighting model.
  43.  *  Objects are drawn using a grey material characteristic. 
  44.  *  A single light source illuminates the objects.
  45.  */
  46. #include <stdlib.h>
  47. #include <GL/glut.h>
  48.  
  49. /*  Initialize material property and light source.
  50.  */
  51. void 
  52. myinit(void)
  53. {
  54.     GLfloat light_ambient[] =
  55.     {0.0, 0.0, 0.0, 1.0};
  56.     GLfloat light_diffuse[] =
  57.     {1.0, 0.0, 0.0, 1.0};
  58.     GLfloat light_specular[] =
  59.     {1.0, 1.0, 1.0, 1.0};
  60. /* light_position is NOT default value */
  61.     GLfloat light_position[] =
  62.     {1.0, 1.0, 1.0, 0.0};
  63.  
  64.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  65.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  66.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  67.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  68.  
  69.     glEnable(GL_LIGHT0);
  70.     glDepthFunc(GL_LESS);
  71.     glEnable(GL_DEPTH_TEST);
  72.     glEnable(GL_LIGHTING);
  73. }
  74.  
  75. void 
  76. display(void)
  77. {
  78.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  79.  
  80.     glPushMatrix();
  81.     glScalef(1.3, 1.3, 1.3);
  82.     glRotatef(20.0, 1.0, 0.0, 0.0);
  83.  
  84.     glPushMatrix();
  85.     glTranslatef(-0.75, 0.5, 0.0);
  86.     glRotatef(90.0, 1.0, 0.0, 0.0);
  87.     glutSolidTorus(0.275, 0.85, 10, 15);
  88.     glPopMatrix();
  89.  
  90.     glPushMatrix();
  91.     glTranslatef(-0.75, -0.5, 0.0);
  92.     glRotatef(270.0, 1.0, 0.0, 0.0);
  93.     glutSolidTetrahedron();
  94.     glPopMatrix();
  95.  
  96.     glPushMatrix();
  97.     glTranslatef(0.75, 0.0, -1.0);
  98.     glutSolidIcosahedron();
  99.     glPopMatrix();
  100.  
  101.     glPopMatrix();
  102.     glFlush();
  103. }
  104.  
  105. void 
  106. myReshape(int w, int h)
  107. {
  108.     glViewport(0, 0, w, h);
  109.     glMatrixMode(GL_PROJECTION);
  110.     glLoadIdentity();
  111.     if (w <= h)
  112.         glOrtho(-2.5, 2.5, -2.5 * (GLfloat) h / (GLfloat) w,
  113.             2.5 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
  114.     else
  115.         glOrtho(-2.5 * (GLfloat) w / (GLfloat) h,
  116.             2.5 * (GLfloat) w / (GLfloat) h, -2.5, 2.5, -10.0, 10.0);
  117.     glMatrixMode(GL_MODELVIEW);
  118. }
  119.  
  120. void 
  121. polygon_mode(int value)
  122. {
  123.     switch (value) {
  124.     case 1:
  125.         glEnable(GL_DEPTH_TEST);
  126.         glEnable(GL_LIGHTING);
  127.         glDisable(GL_BLEND);
  128.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  129.         break;
  130.     case 2:
  131.         glDisable(GL_DEPTH_TEST);
  132.         glDisable(GL_LIGHTING);
  133.         glColor3f(1.0, 1.0, 1.0);
  134.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  135.         glEnable(GL_LINE_SMOOTH);
  136.         glEnable(GL_BLEND);
  137.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  138.         break;
  139.     }
  140.     glutPostRedisplay();
  141. }
  142.  
  143. void 
  144. main_menu(int value)
  145. {
  146.     if (value == 666)
  147.         exit(0);
  148. }
  149.  
  150. /*  Main Loop
  151.  *  Open window with initial window size, title bar, 
  152.  *  RGBA display mode, and handle input events.
  153.  */
  154. int 
  155. main(int argc, char **argv)
  156. {
  157.     int submenu;
  158.  
  159.     glutInit(&argc, argv);
  160.     glutInitWindowPosition(500, 500);
  161.     glutInitWindowSize(500, 500);
  162.     glutCreateWindow(argv[0]);
  163.     myinit();
  164.     glutReshapeFunc(myReshape);
  165.     glutDisplayFunc(display);
  166.     submenu = glutCreateMenu(polygon_mode);
  167.     glutAddMenuEntry("Filled", 1);
  168.     glutAddMenuEntry("Outline", 2);
  169.     glutCreateMenu(main_menu);
  170.     glutAddMenuEntry("Quit", 666);
  171.     glutAddSubMenu("Polygon mode", submenu);
  172.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  173.     glutMainLoop();
  174.     return 0;             /* ANSI C requires main to return int. */
  175. }
  176.